﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using RestSharp;
using xClient.Framework.StaticResource;
using xClient.Framework;

namespace cMES1005.Services
{
    public class ServiceMethod
    {
        public static RestClient restClient = new RestClient(StaticDictionary.Dictionary.ServiceUrl);
        public ServiceMethod() 
        {

        }
        public static ExecutionResult<dynamic> GetVendorList()
        {
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();

            string resource = "/basicdata/vendor";

            var request = new RestRequest(resource, Method.GET);

            request.AddParameter("token", StaticDictionary.Dictionary.Token);

            IRestResponse response = restClient.Execute(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = response.Content;

                dynamic model = JsonConvert.DeserializeObject<dynamic>(content);

                result.Status = model.status;
                result.Message = model.message;
                result.Data = model.datas;
            }
            else
            {
                result.Status = false;

                if (response.StatusCode == 0)
                {
                    result.Message = response.ErrorMessage + ":" + response.ErrorException;
                }
                else
                {
                    result.Message = "Resource Not Found： " + response.StatusCode + ":" + response.StatusDescription + ":";
                }
            }

            return result;
        }
        public static ExecutionResult<dynamic> GetVendorByID(string vcode)
        {
            ExecutionResult<dynamic> result = new ExecutionResult<dynamic>();

            string resource = $"/basicdata/vendor/{vcode}";

            var request = new RestRequest(resource, Method.GET);

            request.AddParameter("token", StaticDictionary.Dictionary.Token);

            IRestResponse response = restClient.Execute(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = response.Content;

                dynamic model = JsonConvert.DeserializeObject<dynamic>(content);

                result.Status = model.status;
                result.Message = model.message;
                result.Data = model.datas;

            }
            else
            {
                result.Status = false;

                if (response.StatusCode == 0)
                {
                    result.Message = response.ErrorMessage + ":" + response.ErrorException;
                }
                else
                {
                    result.Message = "Resource Not Found： " + response.StatusCode + ":" + response.StatusDescription + ":";
                }
            }

            return result;
        }
        public static ExecutionResult AddVendor(string viewModel)
        {
            ExecutionResult result = new ExecutionResult();

            string resource = "/basicdata/vendor/add";

            var request = new RestRequest(resource, Method.POST);

            request.AddParameter("token", StaticDictionary.Dictionary.Token);

            request.AddParameter("MODEL", viewModel);

            IRestResponse response = restClient.Execute(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = response.Content;

                dynamic model = JsonConvert.DeserializeObject<dynamic>(content);

                result.Status = model.status;
                result.Message = model.message;
                result.ExecuteCount = model.executeCount;

            }
            else
            {
                result.Status = false;

                if (response.StatusCode == 0)
                {
                    result.Message = response.ErrorMessage + ":" + response.ErrorException;
                }
                else
                {
                    result.Message = "Resource Not Found： " + response.StatusCode + ":" + response.StatusDescription + ":";
                }
            }

            return result;
        }
        public static ExecutionResult UpdateVendor(string viewModel)
        {
            ExecutionResult result = new ExecutionResult();

            string resource = "/basicdata/vendor/update";

            var request = new RestRequest(resource, Method.POST);

            request.AddParameter("token", StaticDictionary.Dictionary.Token);

            request.AddParameter("MODEL", viewModel);

            IRestResponse response = restClient.Execute(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = response.Content;

                dynamic model = JsonConvert.DeserializeObject<dynamic>(content);

                result.Status = model.status;
                result.Message = model.message;
                result.ExecuteCount = model.executeCount;

            }
            else
            {
                result.Status = false;

                if (response.StatusCode == 0)
                {
                    result.Message = response.ErrorMessage + ":" + response.ErrorException;
                }
                else
                {
                    result.Message = "Resource Not Found： " + response.StatusCode + ":" + response.StatusDescription + ":";
                }
            }

            return result;
        }
        public static ExecutionResult DeleteVendor(string vcode)
        {
            ExecutionResult result = new ExecutionResult();

            string resource = $"/basicdata/vendor/delete/{vcode}";

            var request = new RestRequest(resource, Method.POST);

            request.AddParameter("token", StaticDictionary.Dictionary.Token);

            IRestResponse response = restClient.Execute(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var content = response.Content;

                dynamic model = JsonConvert.DeserializeObject<dynamic>(content);

                result.Status = model.status;
                result.Message = model.message;
                result.ExecuteCount = model.executeCount;

            }
            else
            {
                result.Status = false;

                if (response.StatusCode == 0)
                {
                    result.Message = response.ErrorMessage + ":" + response.ErrorException;
                }
                else
                {
                    result.Message = "Resource Not Found： " + response.StatusCode + ":" + response.StatusDescription + ":";
                }
            }

            return result;
        }

    }
}
